const BASE = "https://api-takumi-record.mihoyo.com/game_record/app/honkai3rd/api";
const API = {
    "往事乐土": `${BASE}/godWar?server={{serverid}}&role_id={{roleid}}`,
    "我的女武神": `${BASE}/characters?server={{serverid}}&role_id={{roleid}}`,
    "数据总览": `${BASE}/index?server={{serverid}}&role_id={{roleid}}`,
    "深渊战报_超弦空间": `${BASE}/newAbyssReport?server={{serverid}}&role_id={{roleid}}`,
    "深渊战报_量子奇点": `${BASE}/oldAbyssReport?server={{serverid}}&role_id={{roleid}}&abyss_type=1`,
    "深渊战报_迪拉克之海": `${BASE}/oldAbyssReport?server={{serverid}}&role_id={{roleid}}&abyss_type=2`,
    "深渊战报_latest": `${BASE}/latestOldAbyssReport?server={{serverid}}&role_id={{roleid}}`,
    "一周成绩单1": `${BASE}/weeklyReport?server={{serverid}}&role_id={{roleid}}`,
    "战场战报": `${BASE}/battleFieldReport?server={{serverid}}&role_id={{roleid}}`,
    "常用工具": `${BASE}/tools`,
    "一周成绩单": `https://api.mihoyo.com/bh3-weekly_finance/api/index?game_biz=bh3_cn&bind_uid={{roleid}}&bind_region={{serverid}}`,
    "获取自己角色": "https://api-takumi-record.mihoyo.com/binding/api/getUserGameRolesByCookie",
    "获取他人角色": `https://api-takumi-record.mihoyo.com/game_record/app/card/wapi/getGameRecordCard?uid={{mysuid}}`,
    "上月手账": `https://api.mihoyo.com/bh3-weekly_finance/api/getLastMonthInfo?game_biz=bh3_cn&bind_uid={{roleid}}&bind_region={{serverid}}`,
    "本月手账": `https://api.mihoyo.com/bh3-weekly_finance/api/index?game_biz=bh3_cn&bind_uid={{roleid}}&bind_region={{serverid}}`,
    "水晶明细": `https://api.mihoyo.com/bh3-weekly_finance/api/getHcoinRecords?page=1&limit=20&game_biz=bh3_cn&bind_uid={{roleid}}&bind_region={{serverid}}`,
    "星石明细": `https://api.mihoyo.com/bh3-weekly_finance/api/getStarRecords?page=1&limit=20&game_biz=bh3_cn&bind_uid={{roleid}}&bind_region={{serverid}}`,
};
class MysApi {
    constructor(server_id, role_id, mysid=null) {
        this.server = server_id;
        this.uid = role_id;
        if (mysid !== null) {
            try {
                this.mid = parseInt(mysid).toString();
                this.getrole = this.generate("获取他人角色", this.mid);
            } catch (error) {
                throw new Error(`${mysid}\n米游社ID格式不对`);
            }
        }
        this.godWar = this.generate("往事乐土");
        this.valkyrie = this.generate("我的女武神");
        this.index = this.generate("数据总览");
        this.newAbyss = this.generate("深渊战报_超弦空间");
        this.oldAbyss_quantum = this.generate("深渊战报_量子奇点");
        this.oldAbyss_dirac = this.generate("深渊战报_迪拉克之海");
        this.oldAbyss_lastest = this.generate("深渊战报_latest");
        this.weekly = this.generate("一周成绩单");
        this.battleField = this.generate("战场战报");
        this.getself = this.generate("获取自己角色");

        this._for_iter = [
            this.godWar,//往事乐土
            this.valkyrie,//我的女武神
            this.index,//数据总览
            this.newAbyss,//深渊战报_超弦空间
            this.oldAbyss_lastest,//深渊战报_latest
            this.weekly,//一周成绩单
            this.battleField,//战场战报
        ];
    }

    generate(typename, ...ids) {
        // typename: URL类型; ids: 3种id
        // todo: change *ids to details
        let url_origin = API[typename];
        let sid, rid, mid;
        if (ids.length === 2) {
            sid = ids[0];
            rid = ids[1];
            mid = "";
        } else if (ids.length === 1) {
            sid = "";
            rid = "";
            mid = ids[0];
        } else {
            sid = this.server;
            rid = this.uid;
            mid = "";
        }
        return url_origin.replace('{{serverid}}', sid)
                         .replace('{{roleid}}', rid)
                         .replace('{{mysuid}}', mid);
    }

    *[Symbol.iterator]() {
        yield* this._for_iter;
    }
}

export { MysApi };
